package com.nkl.page.dao;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import com.nkl.common.dao.BaseDao;
import com.nkl.common.util.StringUtil;
import com.nkl.page.domain.UserNo;

public class UserNoDao {

	public int addUserNo(UserNo userNo, Connection conn){
		String sql = "INSERT INTO user_no(no_id,user_no,real_name) values(null,?,?)";
		Object[] params = new Object[] {
			userNo.getUser_no(),
			userNo.getReal_name()

		};
		return BaseDao.executeUpdate(sql, params, conn );
	}

	public int delUserNo(String no_id, Connection conn){
		String sql = "DELETE FROM user_no WHERE no_id=?";

		Object[] params = new Object[] { new Integer(no_id)};
		return BaseDao.executeUpdate(sql, params, conn);
	}

	public int delUserNos(String[] no_ids, Connection conn){
		StringBuilder sBuilder = new StringBuilder();
		for (int i = 0; i <no_ids.length; i++) {
			sBuilder.append("?");
			if (i !=no_ids.length-1) {
				sBuilder.append(",");
			}
		}
		String sql = "DELETE FROM user_no WHERE no_id IN(" +sBuilder.toString()+")";

		Object[] params = no_ids;

		return BaseDao.executeUpdate(sql, params, conn);	}

	public int updateUserNo(UserNo userNo, Connection conn){
		StringBuilder sBuilder = new StringBuilder();
		sBuilder.append("UPDATE user_no SET no_id = " + userNo.getNo_id() +" ");

		sBuilder.append(" where no_id = " + userNo.getNo_id());

		Object[] params = null;
		return BaseDao.executeUpdate(sBuilder.toString(), params, conn);	}

	public UserNo getUserNo(UserNo userNo, Connection conn){
		UserNo _userNo=null;
		StringBuilder sBuilder = new StringBuilder();
		sBuilder.append("SELECT * FROM user_no WHERE no_id="+userNo.getNo_id());

		List<Object> list = BaseDao.executeQuery(UserNo.class.getName(), sBuilder.toString(), null, conn);
		if (list != null && list.size() > 0) {
			 _userNo = (UserNo)list.get(0);
		}
		return _userNo;
	}
	
	public List<UserNo>  listUserNos(UserNo userNo, Connection conn){
		List<UserNo> userNos = null;
		StringBuilder sBuilder = new StringBuilder();
		sBuilder.append("SELECT * FROM (");
		sBuilder.append("SELECT * FROM user_no WHERE 1=1");
		if (!StringUtil.isEmptyString(userNo.getUser_no())) {
			sBuilder.append(" and user_no='"+userNo.getUser_no()+"'");
		}
		
		sBuilder.append(" order by no_id asc) t");
		
		if (userNo.getStart() != -1) {
			sBuilder.append(" limit " + userNo.getStart() + "," + userNo.getLimit());
		}

		List<Object> list = BaseDao.executeQuery(UserNo.class.getName(), sBuilder.toString(), null, conn);
		if (list != null && list.size() > 0) {
			userNos = new ArrayList<UserNo>();
			for (Object object : list) {
				userNos.add((UserNo)object);
			}
		}
		return userNos;
	}
	
	public int  listUserNosCount(UserNo userNo, Connection conn){
		int sum = 0;
		StringBuilder sBuilder = new StringBuilder();
		sBuilder.append("SELECT count(*) FROM user_no WHERE 1=1");
		if (!StringUtil.isEmptyString(userNo.getUser_no())) {
			sBuilder.append(" and user_no='"+userNo.getUser_no()+"'");
		}

		long count = (Long)BaseDao.executeQueryObject(sBuilder.toString(), null, conn);
		sum = (int)count;
		return sum;
	}

}
